package cn.ronghuanet.car.service.impl;

import cn.ronghuanet.car.dto.CarResourcesDTO;
import cn.ronghuanet.car.domain.CarResources;
import cn.ronghuanet.car.mapper.CarResourcesMapper;
import cn.ronghuanet.car.service.ICarResourcesService;
import cn.ronghuanet.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiong
 * @since 2023-07-03
 */
@Service
public class CarResourcesServiceImpl extends BaseServiceImpl<CarResources> implements ICarResourcesService {

    @Autowired
    private CarResourcesMapper carResourcesMapper;

    @Transactional
    @Override
    public void batchSave(CarResourcesDTO dto) {
        // 循环dto.list,将数据存入数据库
        // 思考? 如果这个carId之前在数据库已经有资源数据的话,怎么办??
        // 1 先删除该CarId所对应的资源信息
        carResourcesMapper.deleteByCarId(dto.getCarId());

        // 2 新增该carId所对应的资源信息
        // 方案一:循环dto.list,每次循环都调用新增方法 - 对数据库压力太大了
        // 方案二:批量新增 采纳这种
        carResourcesMapper.batchInsert(dto.getCarId(),dto.getList());
    }

    @Override
    public List<CarResources> getCarResourceByCarId(Long carId) {
        return carResourcesMapper.getCarResourceByCarId(carId);
    }
}
